#! /usr/bin/env python

import argparse
import logging
import sys
import os
reload(sys)
sys.setdefaultencoding('utf-8')

parser = argparse.ArgumentParser(description='EzChip Result check')
parser.add_argument('-l', '--log', nargs="+", dest="log", \
                  help="set the sim log for analysis")
parser.add_argument('-p', '--pass_cfg', nargs='+', dest="pass_cfg", default=None, \
                  help="set the pass cfg ")
parser.add_argument('-f', '--fail_cfg', nargs='+', dest="fail_cfg", default=None, \
                  help="set the fail cfg ")
parser.add_argument('-i', '--ignore_cfg', nargs='+', dest="ignore_cfg", default=None, \
                  help="set the ignore cfg ")
parser.add_argument('-d', '--debug', action='store_true', dest="debug", default=False, \
                  help="enable the check result debug mode")
args=parser.parse_args()

logger = logging.getLogger(__name__)

if args.debug: 
  log_mode = logging.DEBUG
else:
  log_mode = logging.INFO
logger.setLevel(level = log_mode)

handler = logging.FileHandler("check.log.")
handler.setLevel(log_mode)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
 
console = logging.StreamHandler()
console.setLevel(log_mode)
 
logger.addHandler(handler)
logger.addHandler(console)

assert args.log !=None, "No testcase sim.log set"
assert os.path.isfile("%s"%args.log[0]), "the log : %s not exists "%(args.log)
assert args.pass_cfg !=None, "No pass cfg sim.log set"
assert os.path.isfile("%s"%args.pass_cfg[0]), "the pass cfg : %s not exists "%(args.pass_cfg)
assert args.fail_cfg !=None, "No fail cfg set"
assert os.path.isfile(""%args.fail_cfg[0]), "the fail log : %s not exists "%(args.fail_cfg)
assert args.ignore_cfg !=None, " No ignore log set"
assert os.path.isfile("%s"%args.ignore_cfg[0]), "the ignore log  : %s not exists "%(args.ignore_cfg)
pass_log = []
pass_cnt = {}
fail_log =[]
ignore_log=[]
pass_rules = []
fail_rules = []
ignore_rules = []
error_cnt = 0

# get the pass rules from pass cfg 
with open("%s"%args.pass_cfg[0], "r") as f:
    for i in f.readlines():
      pass_rules.append(i.replace("\n", ""))

# get the fail rules from fail cfg 
with open("%s"%args.fail_cfg[0], "r") as f:
    for i in f.readlines():
      fail_rules.append(i.replace("\n", ""))

# get the ignore rules from ignore cfg 
with open("%s"%args.ignore_cfg[0], "r") as f:
    for i in f.readlines():
      ignore_rules.append(i.replace("\n", ""))

#check the log 
with open(args.log, "r") as f:
    for i in f.readlines():
      content = i.replace("\n", "")
      content_tmp = content
      for j in ignore_rules:
        if j in content:
          content = content.strip(j)
      if content != content_tmp:
         ignore_log.append(i)
      for j in pass_rules:
        if j in content:
            pass_log.append(i)
            pass_cnt[j] = 1
      for j in fail_rules:
        if j in content:
           fail_log.append(i)

if len(pass_cnt.keys()) != len(pass_rules):
   error_cnt +=1

if len(fail_log):
   error_cnt +=1

if error_cnt:
    logging.info("\033[1;31mTEST CASE FAIL\033[0m\n")
else:
    logging.info("\033[1;32mTEST CASE PASS\033[0m\n")

if args.debug:
   pass_content = "\n".join(pass_log)
   fail_content = "\n".join(fail_log)
   ignore_content = "\n".join(ignore_log)
   logger.debug("\033[1;32m%s\033[0m\n"%(pass_content))
   logger.debug("\033[1;31m%s\033[0m\n"%(fail_content))
   logger.debug("\033[1;33m%s\033[0m\n"%(ignore_content))

sys.exit(error_cnt)